<?php require_once('../Connections/aciitu.php'); ?>
<?php
// Load the common classes
require_once('../includes/common/KT_common.php');

// Load the tNG classes
require_once('../includes/tng/tNG.inc.php');

// Make a transaction dispatcher instance
$tNGs = new tNG_dispatcher("../");

// Make unified connection variable
$conn_aciitu = new KT_connection($aciitu, $database_aciitu);

// Start trigger
$formValidation = new tNG_FormValidation();
$tNGs->prepareValidation($formValidation);
// End trigger

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_aciitu, $aciitu);
$query_RsSite = "SELECT * FROM site";
$RsSite = mysql_query($query_RsSite, $aciitu) or die(mysql_error());
$row_RsSite = mysql_fetch_assoc($RsSite);
$totalRows_RsSite = mysql_num_rows($RsSite);


// Make a login transaction instance
$loginTransaction = new tNG_login($conn_aciitu);
$tNGs->addTransaction($loginTransaction);
// Register triggers
$loginTransaction->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "kt_login1");
$loginTransaction->registerTrigger("BEFORE", "Trigger_Default_FormValidation", 10, $formValidation);
$loginTransaction->registerTrigger("END", "Trigger_Default_Redirect", 99, "{kt_login_redirect}");
// Add columns
$loginTransaction->addColumn("kt_login_user", "STRING_TYPE", "POST", "kt_login_user");
$loginTransaction->addColumn("kt_login_password", "STRING_TYPE", "POST", "kt_login_password");
// End of login transaction instance

// Execute all the registered transactions
$tNGs->executeTransactions();

// Get the transaction recordset
$rscustom = $tNGs->getRecordset("custom");
$row_rscustom = mysql_fetch_assoc($rscustom);
$totalRows_rscustom = mysql_num_rows($rscustom);
?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['kt_login_user'])) {
  $loginUsername=$_POST['kt_login_user'];
  $password=$_POST['kt_login_password'];
  $MM_fldUserAuthorization = "id_do_nivel";
  $MM_redirectLoginSuccess = "index.php";
  $MM_redirectLoginFailed = "login.php";
  $MM_redirecttoReferrer = true;
  mysql_select_db($database_aciitu, $aciitu);
  	
  $LoginRS__query=sprintf("SELECT usuario_user, senha_user, id_do_nivel FROM usuarios WHERE usuario_user=%s AND senha_user=%s AND status_user = 1",
  GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
   
  $LoginRS = mysql_query($LoginRS__query, $aciitu) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
    
    $loginStrGroup  = mysql_result($LoginRS,0,'id_do_nivel');
    
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;	      

    if (isset($_SESSION['PrevUrl']) && true) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>.: <?php echo $row_RsSite['site']; ?> | Administração | Pagina de Login :.</title>
<link href="../includes/skins/mxkollection3.css" rel="stylesheet" type="text/css" media="all" />
<script src="../includes/common/js/base.js" type="text/javascript"></script>
<script src="../includes/common/js/utility.js" type="text/javascript"></script>
<script src="../includes/skins/style.js" type="text/javascript"></script>
<?php echo $tNGs->displayValidationRules();?>
</head>

<body onLoad="document.getElementById('kt_login_user').focus()">
<br /><br /><br />
<table width="334" height="205" border="0" align="center" cellpadding="0" cellspacing="0" background="../imagens/loginsite.jpg" style="background-position:top; background-repeat:no-repeat;">


<form method="POST" id="form" class="KT_tngformerror" action="<?php echo $loginFormAction; ?>">

<tr>
  <td width="60">&nbsp;</td>
  <td width="42">&nbsp;</td>
  <td width="6">&nbsp;</td>
  <td width="226">&nbsp;</td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td height="37">&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td height="25">&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td valign="top" align="left">
  <input name="kt_login_user" type="text" id="kt_login_user" value="<?php echo KT_escapeAttribute($row_rscustom['kt_login_user']); ?>" size="20" maxlength="20" style="background-color:#000000; font-family:Arial; font-size:11px; color:#FFFFFF; font-weight:bold;" /></td>
</tr>
<tr>
  <td height="27">&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td valign="top" align="left">
  <input name="kt_login_password" type="password" id="kt_login_password" size="20" maxlength="12" style="background-color:#000000; font-family:Arial; font-size:11px; color:#FFFFFF;" /></td>
</tr>
<tr>
  <td height="28">&nbsp;</td>
  <td valign="top" align="left"><input type="submit" name="kt_login1" id="kt_login1" value="Entrar" style="background-color:#FFFFFF; border-color:#000000; color:#000000; font-family:Arial; font-size:10px" /></td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
</tr>
<?php
	echo $tNGs->getLoginMsg();
?>
<?php
	echo $tNGs->getErrorMsg();
?>

<p>&nbsp;</p>
</form>
</table>
</body>
</html>
<?php
mysql_free_result($RsSite);
?>
